import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:gourmet/utils/colors.dart';
import 'package:gourmet/utils/styles.dart';
import 'package:gourmet/utils/svg.dart';

class HomeMessageController extends GetxController {}

class HomeMessageView extends GetView<HomeMessageController> {
  const HomeMessageView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        systemOverlayStyle: SystemUiOverlayStyle.dark,
        automaticallyImplyLeading: false,
        centerTitle: true,
        title: Text(
          '消息',
          style: StylesUtils.customTextStyle(
              fontSize: StylesUtils.fontSize16,
              fontWeight: FontWeight.w700,
              color: ColorsUtils.color0000),
        ),
        actions: [
          IconButton(
              onPressed: () async {
                // openDialog(context);
                int? result = await Get.dialog<int?>(
                    Dialog(
                      alignment: Alignment.center,
                      child: Container(
                        constraints: const BoxConstraints.expand(width: 280, height: 135),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(10.r),
                            boxShadow: [
                              //0px 0px 10px rgba(0, 0, 0, 0.0980392156862745)
                              BoxShadow(
                                  color: ColorsUtils.rgba(0, 0, 0, 0.0980392156862745),
                                  offset: Offset.zero,
                                  blurStyle: BlurStyle.outer,
                                  blurRadius: 10,
                                  spreadRadius: 0)
                            ],
                            border: Border.all(color: ColorsUtils.rgba(242, 242, 242, 1))),
                        child: Column(
                          children: [
                            Expanded(
                                child: Center(
                              child: Text(
                                '标记所有消息为已读？',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0000, fontSize: StylesUtils.fontSize18),
                              ),
                            )),
                            SizedBox(
                              height: 50,
                              child: CustomPaint(
                                size: const Size.fromHeight(50),
                                painter: _MyDialogLine(),
                                child: Row(
                                  children: [
                                    Expanded(
                                        child: InkWell(
                                      onTap: () {
                                        Get.back(result: 1);
                                      },
                                      child: Center(
                                        child: Text(
                                          '取消',
                                          style: StylesUtils.customTextStyle(
                                              fontSize: StylesUtils.fontSize16,
                                              color: ColorsUtils.color0000),
                                        ),
                                      ),
                                    )),
                                    Expanded(
                                        child: InkWell(
                                      onTap: () {
                                        Get.back(result: 2);
                                      },
                                      child: Center(
                                        child: Text(
                                          '确定',
                                          style: StylesUtils.customTextStyle(
                                              fontSize: StylesUtils.fontSize16,
                                              color: ColorsUtils.colorFf6e),
                                        ),
                                      ),
                                    ))
                                  ],
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    barrierColor: Colors.black.withOpacity(0.298039));
                if (result == 1) {
                  // 取消
                  Get.log('result = $result');
                } else if (result == 2) {
                  // 确定
                  Get.log('result = $result');
                }
              },
              icon: SvgUtils.assetName('u7570', width: 18.w, height: 20.h))
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      SvgUtils.assetName('u7575', width: 40, height: 40),
                      Positioned(
                          top: -2,
                          right: -2,
                          child: CustomPaint(
                            size: const Size(12, 12),
                            painter: _MyCustomPainter(),
                          ))
                    ],
                  ),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: CustomPaint(
                      painter: _MyUnderLine(),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '系统通知',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0000, fontSize: StylesUtils.fontSize15),
                              ),
                              Text(
                                '23小时前',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize12, color: ColorsUtils.colorCccc),
                              )
                            ],
                          ),
                          Container(
                            padding: const EdgeInsets.only(top: 10, bottom: 15),
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '您的新昵称因涉及违规，审核未通过。',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color7979, fontSize: StylesUtils.fontSize13),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          )
                        ],
                      ),
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      SvgUtils.assetName('u7582', width: 40, height: 40),
                      Positioned(
                          top: -2,
                          right: -2,
                          child: CustomPaint(
                            size: const Size(12, 12),
                            painter: _MyCustomPainter(),
                          ))
                    ],
                  ),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: CustomPaint(
                      painter: _MyUnderLine(),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '订单通知',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0000, fontSize: StylesUtils.fontSize15),
                              ),
                              Text(
                                '5分钟前',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize12, color: ColorsUtils.colorCccc),
                              )
                            ],
                          ),
                          Container(
                            padding: const EdgeInsets.only(top: 10, bottom: 15),
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '附近暂无配送员接单，订单交易关闭。',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color7979, fontSize: StylesUtils.fontSize13),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          )
                        ],
                      ),
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      SvgUtils.assetName('u7589', width: 40, height: 40),
                    ],
                  ),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: CustomPaint(
                      painter: _MyUnderLine(),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                '活动通知',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0000, fontSize: StylesUtils.fontSize15),
                              ),
                              Text(
                                '09-26',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize12, color: ColorsUtils.colorCccc),
                              )
                            ],
                          ),
                          Container(
                            padding: const EdgeInsets.only(top: 10, bottom: 15),
                            alignment: Alignment.centerLeft,
                            child: Text(
                              '双十一全平台商家均享订单满100元',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color7979, fontSize: StylesUtils.fontSize13),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          )
                        ],
                      ),
                    ),
                  ))
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Future<void> openDialog(BuildContext context) async {
    int? result = await showDialog<int>(
        context: context,
        builder: (c) {
          return Dialog(
            alignment: Alignment.center,
            child: Container(
              constraints: const BoxConstraints.expand(width: 280, height: 135),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10.r),
                  boxShadow: [
                    //0px 0px 10px rgba(0, 0, 0, 0.0980392156862745)
                    BoxShadow(
                        color: ColorsUtils.rgba(0, 0, 0, 0.0980392156862745),
                        offset: Offset.zero,
                        blurStyle: BlurStyle.outer,
                        blurRadius: 10,
                        spreadRadius: 0)
                  ],
                  border: Border.all(color: ColorsUtils.rgba(242, 242, 242, 1))),
              child: Column(
                children: [
                  Expanded(
                      child: Center(
                    child: Text(
                      '标记所有消息为已读？',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color0000, fontSize: StylesUtils.fontSize18),
                    ),
                  )),
                  SizedBox(
                    height: 50,
                    child: CustomPaint(
                      size: const Size.fromHeight(50),
                      painter: _MyDialogLine(),
                      child: Row(
                        children: [
                          Expanded(
                              child: InkWell(
                            onTap: () {
                              Get.back(result: 1);
                            },
                            child: Center(
                              child: Text(
                                '取消',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize16, color: ColorsUtils.color0000),
                              ),
                            ),
                          )),
                          Expanded(
                              child: InkWell(
                            onTap: () {
                              Get.back(result: 2);
                            },
                            child: Center(
                              child: Text(
                                '确定',
                                style: StylesUtils.customTextStyle(
                                    fontSize: StylesUtils.fontSize16, color: ColorsUtils.colorFf6e),
                              ),
                            ),
                          ))
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          );
        },
        barrierColor: Colors.black.withOpacity(0.298039));
    if (result == 1) {
      // 取消
      Get.log('result = $result');
    } else if (result == 2) {
      // 确定
      Get.log('result = $result');
    }
  }
}

class _MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.white
      ..isAntiAlias = true
      ..style = PaintingStyle.fill;
    Offset c = Offset(size.width / 2, size.height / 2);
    canvas.drawCircle(c, size.width / 2, paint);
    paint.color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf64);
    canvas.drawCircle(c, size.width / 2 - 1.5, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}

class _MyUnderLine extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorF5f5)
      ..isAntiAlias = true
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;
    Offset p1 = Offset(0, size.height);
    Offset p2 = Offset(size.width, size.height);
    canvas.drawLine(p1, p2, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}

class _MyDialogLine extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2)
      ..isAntiAlias = true
      ..strokeWidth = 2
      ..style = PaintingStyle.stroke;
    Offset p = Offset(size.width, 0);
    canvas.drawLine(Offset.zero, p, paint);
    paint.strokeWidth = 1;
    canvas.drawLine(Offset(size.width / 2, 0), Offset(size.width / 2, size.height), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
